package fourmiefight.ihm;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;

import org.dyn4j.dynamics.Body;
import org.dyn4j.dynamics.BodyFixture;
import org.dyn4j.geometry.Circle;
import org.dyn4j.geometry.Convex;
import org.dyn4j.geometry.Vector2;

import fourmiefight.abstractionlib2d.IHMItem;

public class FourmiView extends IHMItem 
{
    protected Color color;
    private int x;
    private int y;

	public FourmiView() 
    {
    	super();
        this.color = Color.BLACK;
    }
   
    public void render(Graphics2D g, Body body) 
    {
        AffineTransform ot = g.getTransform();
       
        AffineTransform lt = new AffineTransform();
        lt.translate(body.getTransform().getTranslationX() * SCALE, body.getTransform().getTranslationY() * SCALE);
        lt.rotate(body.getTransform().getRotation());
       
        g.transform(lt);
        for (int i = 0 ; i < body.getFixtureCount() ; i++)
        {
        	BodyFixture fixture = body.getFixture(i);
            Convex convex = fixture.getShape();
            if (convex instanceof Circle) 
            {
            	Circle c = (Circle) convex;
                double r = c.getRadius();
                Vector2 cc = c.getCenter();
                x = (int)Math.ceil((cc.x - r) * SCALE);
                y = (int)Math.ceil((cc.y - r) * SCALE);
                int w = (int)Math.ceil(r * 2 * SCALE);
                g.setColor(this.color);
                g.fillOval(x, y, w, w);
                g.setColor(this.color.darker());
                g.drawOval(x, y, w, w);
            }
        }
        g.setTransform(ot);
    }
    
    public int getX() {
    	return x;
    }
    
    public int getY() {
    	return y;
    }
}
